﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using PrismMetroSample.Infrastructure;
using PrismMetroSample.Infrastructure.Constants;
using PrismMetroSample.Infrastructure.Events;
using PrismMetroSample.Infrastructure.Models;
using PrismMetroSample.Infrastructure.Services;
using System;
using System.Collections.Generic;
using System.Text;

namespace PrismMetroSample.PatientModule.ViewModels
{
    public class PatientListViewModel : BindableBase
    {
        private IApplicationCommands _applicationCommands;
        public IApplicationCommands ApplicationCommands
        {
            get 
            {
                return _applicationCommands;
            }
            set 
            {
                SetProperty(ref _applicationCommands, value);
            }
        }

        private List<Patient> _allPatients;
        public List<Patient> AllPatients
        {
            get 
            {
                return _allPatients;
            }
            set 
            {
                SetProperty(ref _allPatients, value);
            }
        }

        private DelegateCommand<Patient> _mouseDoubleClickCommand;
        public DelegateCommand<Patient> MouseDoubleClickCommand =>
            _mouseDoubleClickCommand ?? (_mouseDoubleClickCommand = new DelegateCommand<Patient>(ExecuteMouseDoubleClickCommand));

        IEventAggregator _ea;

        IPatientService _patientService;


        public PatientListViewModel(IPatientService patientService, IEventAggregator ea, IApplicationCommands applicationCommands)
        {
            _ea = ea;
            this.ApplicationCommands = applicationCommands;
            _patientService = patientService;
            this.AllPatients = _patientService.GetAllPatients();
        }


        private void ExecuteMouseDoubleClickCommand(Patient patient)
        {
            //打开窗体
            this.ApplicationCommands.ShowCommand.Execute(FlyoutNames.PatientDetailFlyout);

            //发布消息
            _ea.GetEvent<PatientSentEvent>().Publish(patient);

        }
    }
}
